//
//  CellListViewController.m
//  LianXi7.11
//
//  Created by apple on 2017/7/12.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "CellListViewController.h"

@interface CellListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableV;

@property (nonatomic, strong) UIButton *btnTable;

@property (nonatomic, assign) BOOL isYES;

@property (nonatomic, strong) NSMutableArray *sectionMutArr;
@property (nonatomic, strong) NSMutableArray *seclectMutArr;
@property (nonatomic, strong) NSMutableArray *rowMutArr;

@end

@implementation CellListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableV];
    //    self.tableV.delegate = self;
    //    self.tableV.dataSource = self;
    
    self.sectionMutArr = [NSMutableArray arrayWithObjects:@"一号", @"二号", @"三号", @"四号", nil];
    self.rowMutArr = [NSMutableArray arrayWithObjects:@"3", @"4", @"2", @"5", nil];
    self.seclectMutArr = [NSMutableArray arrayWithObjects:@"0", @"0", @"0", @"0", nil];
    
    self.isYES = YES;
    
    [self createBtnBack];
}

- (void)createTableV{
    
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 400) style:UITableViewStylePlain];
    
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    self.tableV.backgroundColor = [UIColor whiteColor];
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_tableV];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    cell.textLabel.text = @"haha";
    
    return cell;
}


/**
 给header上加一层button

 @param tableView tableV
 @param section 每个section
 @return button
 */
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIButton *btn = [[UIButton alloc] init];
    
//    btn.backgroundColor = [UIColor blueColor];
    
    btn.tag = 1000 + section;
    
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    if (section == 0) {
        
        btn.backgroundColor = [UIColor blueColor];
        
    }else if (section == 1){
    
        btn.backgroundColor = [UIColor redColor];
        
    }else if (section == 2){
    
        btn.backgroundColor = [UIColor greenColor];
        
    }else{
    
        btn.backgroundColor = [UIColor purpleColor];
    }
    
    return btn;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 44;
}


/**
 判断一下Row是不是应该有

 @param tableView tableV
 @param section section
 @return 返回值根据判断的来
 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([_seclectMutArr[section] isEqualToString:@"1"]) {
        
        return [_rowMutArr[section] integerValue];
        
    }else{
        
        return 0;
        
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return _sectionMutArr.count;
}


/**
 button点击事件

 @param sender button
 */
- (void)btnClick:(UIButton *)sender{
    
    long int i = sender.tag -1000;
    
    if ([_seclectMutArr[i] isEqualToString:@"0"]) {
        //点击的section在数组里从0变成1，代表cell该出现了
        [self.seclectMutArr replaceObjectAtIndex:i withObject:@"1"];
        //刷新使cell出现
        [self.tableV reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
        
    }else{
        //点击的section在数组里从1变成0，代表cell该消失了
        [self.seclectMutArr replaceObjectAtIndex:i withObject:@"0"];
        //刷新使cell消失
        [self.tableV reloadSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationFade];
    }
}

- (void)createBtnBack{
    
    UIButton *btnBack = [[UIButton alloc] initWithFrame:CGRectMake(30, 440, 50, 50)];
    
    [btnBack setTitle:@"BACK" forState:UIControlStateNormal];
    [btnBack setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    btnBack.backgroundColor = [UIColor greenColor];
    
    [btnBack addTarget:self action:@selector(btnBack) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:btnBack];
    
}

- (void)btnBack{
    
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
